<?

/*	CalendarBasic OBJECT
*	Comment here
*	
*					*/


class CalendarCurrent extends MOD {

	var $days =		array(
		"nl" =>		array( 0, "zondag", "maandag", "dinsdag", "woensdag", "donderdag", "vrijdag", "zaterdag" )
	);
	
	function __construct( $vars ) {
		global $CAL, $Calnode;
		
		$this->_addvars( $vars );
		$this->_ref();

#		Load Calendar class.
		$CAL = new CALENDAR();
	}
	
	/*public function construct() {
		global $DB, $CAL, $Page, $Cal;
		

		$Cal = new CALNODE( $this->vars->type, (int) $_GET['calid'] );
		$Page->set_cache( "title", date( "d.m Y", strtotime( $Cal->get( 'date' ))));
	}*/

	
	public function output() {
	
		if( $this->action )
			$nodes = $this->{ "filter_" . $this->action }();
	
		
		if( $nodes ) {
			$this->addclass( $this->action );
			$result = ( $nodes ) ?
				"<ul>" . implode("\n", $nodes ) . "</ul>\n" . $script : "empty.";
		
		}
		
		if( $nodes )	
			return $this->parse( $result );
	
	}	
	
	
	private function filter_current() {
		global $DB, $Object;
		
		if( is_array( $this->vars->objtype)) $this->vars->objtype = implode( ",", $this->vars->objtype ); 
		
		$yesterday 	= date( 'Y-m-d H:i:s', strtotime( 'yesterday' ));
		$now		= date( 'Y-m-d H:i:s', time());
		//$now		= '2010-10-13 22:10:00';
		$response 	= $DB->filter_key(
			$this->vars->type, 
			"date > '" . $yesterday . "' && otype in({$this->vars->objtype}) && active order by mod_calendar.date asc limit 25",
			"ID, otype, objid, span, date, (select <%lang:obj.nameid%> from mod_object as obj where obj.ID=objid) as name"
		);
		
		if( $response ) foreach( $response as $event ) {
			
			$start 	= $event['date'];
			$end	= date( 'Y-m-d H:i:s', strtotime( $event['date'] ) + ( $event['span'] * 60 ));
			
			if( $now > $start && $now < $end ) {
		
				$Cal 	= new OBJECT( $DB->type_r( $event['otype'] ), $event['objid'] );
				$Loc	= new OBJECT( 'location', $event['location'] );
				$nodes[] = "<li>";
				$nodes[] = "<a class='name' href='" . $Cal->uri() . "' title='" .$Cal->get( 'name' ) . "'>" .$Cal->get( 'name' ) . "</a>";
				$nodes[] = "<span class='loc'>" . $Loc->get( 'name' ) . "</span>";
				$nodes[] = "</li>";
			
			}			
		}
		return $nodes;
	}
	
	private function filter_object() {
		global $CAL, $DB, $Object;
		
		$yesterday 	= date( 'Y-m-d H:i:s', strtotime( 'yesterday' ));
		$now		= date( 'Y-m-d H:i:s', time());
		
		//$otype		= $DB->get( 'mod_calendar', (int) $Object->get( 'ID' ), 'otype' );

		if( $Object->type != $this->vars->type )
			if( $Object->obj && $Object->obj->type == $this->vars->type ) $Object = $Object->obj;
			else return null;
			
		$nodes = $CAL->filter_nodes_by_object( "cal.single", $Object->type, (int) $Object->id, "active order by date desc limit " . $this->vars->limit );
		
		$active = ( count( $nodes ) > 1 && $Active = $CAL->get_node( "now()", "cal.single", $Object->type, (int) $Object->id ))?
					$Active->get( "datestr" ) : null;

		if( $nodes )
			foreach( $nodes as $Node )
				$start 	= $Node->get( 'date' );
				$end	= date( 'Y-m-d H:i:s', strtotime( $start ) + ( $Node->get( 'span' ) * 60 ));
				if ( $active == $Node->get( "datestr" ) && $now > $start && $now < $end ) 
					$lines[] = "<li><a href='#' class='active'>" . translate( 'Nu bezig!' ) . "</a><span class='loc'>" . $Node->loc->get( 'name' ) . "</li>";
					
		//print_r( $lines );
		return $lines;
	}

} 

?>


